unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, IdBaseComponent, IdComponent,
  IdTCPConnection, IdTCPClient, IdExplicitTLSClientServerBase, IdFTP,
  Vcl.StdCtrls,IdGlobal, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack,IdAllFTPListParsers;

type
  TForm1 = class(TForm)
    IdFTP1: TIdFTP;
    Memo_1: TMemo;
    Edit_ServAddr: TEdit;
    Edit_ServPort: TEdit;
    Edit_UserName: TEdit;
    Edit_PassWord: TEdit;
    put: TButton;
    Button1: TButton;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    procedure listClick(Sender: TObject);
    procedure IdFTP1Connected(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure putClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation


{$R *.dfm}

procedure TForm1.listClick(Sender: TObject);
var
 I:Integer;
begin
  //
  self.IdFTP1.Host:=Trim(Edit_ServAddr.Text);
  self.IdFTP1.Port:=21;
  self.IdFTP1.Username:=Trim(Edit_UserName.Text);
  self.IdFTP1.Password:=Trim(Edit_PassWord.Text);
  Self.IdFTP1.Passive :=CheckBox1.Checked;

  if self.IdFTP1.Connected then
  begin
    self.IdFTP1.Disconnect;
  end;
  self.IdFTP1.Connect;
  //Self.IdFTP1.IOHandler.DefStringEncoding:=IndyTextEncoding_Default;
  self.IdFTP1.List;
  Memo_1.Lines.Clear;
  if CheckBox2.Checked then
  begin
    for I := 0 to IdFTP1.DirectoryListing.Count-1 do
    begin
      //ShowMessage(AnsiToUtf8(IdFTP1.DirectoryListing.Items[i].FileName));
      Memo_1.Lines.Add(AnsiToUtf8(IdFTP1.DirectoryListing.Items[i].FileName));
    end;
  end;

  for I := 0 to IdFTP1.ListResult.Count-1 do
  begin
    //ShowMessage(AnsiToUtf8(IdFTP1.ListResult.Strings[i]));
    Memo_1.Lines.Add(AnsiToUtf8(IdFTP1.ListResult.Strings[i]));
  end;
end;

procedure TForm1.putClick(Sender: TObject);
var
  MemoryStream:TMemoryStream;
begin
  if not Self.IdFTP1.Connected then
  begin
    Self.IdFTP1.Connect;
  end;
  try
    MemoryStream:=TMemoryStream.Create;
    MemoryStream.LoadFromFile('00001.zip');
    Self.IdFTP1.Put(MemoryStream,'00001.zip');
  finally
    FreeAndNil(MemoryStream);
  end;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  Edit_ServAddr.Text:='192.168.0.73';
  Edit_ServPort.Text:='21';
  Edit_UserName.Text:='yxc';
  Edit_PassWord.Text:='yxc';
end;

procedure TForm1.IdFTP1Connected(Sender: TObject);
begin
  //do not work
  //IdFTP1.IOHandler.DefStringEncoding:=IndyTextEncoding_Default;
end;

end.
